/*
 * Project: Set Sample
 * Copyright (C) 2013 alf.labs gmail com,
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.alflabs.setsample.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import com.alflabs.annotations.NonNull;
import com.alflabs.setsample.R;

public class IntroFragment extends Fragment {

    private boolean mAnimate = true;
    private static final int ACTIVITY_STARTED_FROM_HERE = 1;

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);

        if (state != null) {
            mAnimate = state.getBoolean("mAnimate", true);
        }

        // Tell the framework to try to keep this fragment around during a configuration change.
        setRetainInstance(true);
    }

    @Override
    public void setRetainInstance(boolean retain) {
        super.setRetainInstance(retain);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("mAnimate", mAnimate);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
        View root = inflater.inflate(R.layout.intro_fragment, container, false);
        assert root != null;
        setupButtonListeners(root);
        return root;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ACTIVITY_STARTED_FROM_HERE) {
            mAnimate = false;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAnimate) {
            setupStartupAnimations(getView());
            mAnimate = false;
        }
    }

    private void setupButtonListeners(@NonNull View root) {
        final Activity activity = getActivity();
        assert activity != null;

        setupActivityButton(activity, root, R.id.btnSettings, SettingsActivity.class);
        setupActivityButton(activity, root, R.id.btnHelp,     HelpActivity.class);
        setupActivityButton(activity, root, R.id.btnStats,    SetStatsActivity.class);
        setupActivityButton(activity, root, R.id.btnPlay,     SetActivity.class);
    }

    private void setupActivityButton(@NonNull final Context context, @NonNull View root, int id, @NonNull final Class<?> clazz) {
        View v = root.findViewById(id);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(context, clazz);
                startActivityForResult(i, ACTIVITY_STARTED_FROM_HERE);
            }
        });
    }

    private void setupStartupAnimations(View root) {

        // Don't animate below API 11.
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) return;

        final Activity activity = getActivity();
        assert activity != null;

        Animation anim = AnimationUtils.loadAnimation(activity, R.anim.intro_item_down2x);
        assert anim != null;

        final View v = root.findViewById(R.id.introIcon);
        v.startAnimation(anim);

        anim = AnimationUtils.loadAnimation(activity, R.anim.intro_item_up);
        assert anim != null;

        View v2 = root.findViewById(R.id.btnSettings);
        v2.startAnimation(anim);

        v2 = root.findViewById(R.id.btnHelp);
        v2.startAnimation(anim);

        v2 = root.findViewById(R.id.btnStats);
        v2.startAnimation(anim);

        v2 = root.findViewById(R.id.btnPlay);
        v2.startAnimation(anim);
    }
}
